package com.sctvcloud.bazhong.ui.adapter;

import android.content.Context;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ruihang.generalibrary.ui.adapter.BaseHolderAbsAdapter;
import com.ruihang.generalibrary.ui.util.IAndroidLittleLife;
import com.ruihang.generalibrary.ui.util.LittleLifeManager;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.base.BaseActivity;
import com.sctvcloud.bazhong.base.BaseFragment;
import com.sctvcloud.bazhong.ui.adapter.holder.BaseAbsHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeActiveHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeAnchorEntranceHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeAnchorHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeBannerHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeEntryHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeHotVideoHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeLiveHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeSpecialHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.HomeTopNewsListHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.Hour24TitleHolder;
import com.sctvcloud.bazhong.ui.adapter.holder.News24Holder;
import com.sctvcloud.bazhong.ui.adapter.holder.NewsPromoteHolder;
import com.sctvcloud.bazhong.ui.util.IListShowData;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/7/4.
 */

public class GreatestAdapter extends BaseHolderAbsAdapter<IListShowData, BaseAbsHolder<IListShowData>> {

    public static final int VIEWTYPE_BANNER = 1;//轮播图
    public static final int VIEWTYPE_EXPRESS = 200;//快讯
    public static final int VIEWTYPE_LIVE = 3;//直播
    public static final int VIEWTYPE_POPULAR = 4;//热门视频
    public static final int VIEWTYPE_TITLE_24 = 7;//24小时的title,第一个的
    public static final int VIEWTYPE_TITLE_24_CONTINUE = 6;//24小时的title,中间的
    public static final int VIEWTYPE_NEWS_24 = 5;//24小时的item
    public static final int VIEWTYPE_LUZHOU_NEWS = 8;//泸州市新闻

    public static final int VIEWTYPE_TOP1_NEWS = 9;//头顶第一个新闻
    public static final int VIEWTYPE_TOP2_NEWS = 10;//头顶第二个新闻
    public static final int VIEWTYPE_TOP3_NEWS = 11;//头顶第三个新闻
    public static final int VIEWTYPE_SPECIAL = 12;//专题新闻
    public static final int VIEWTYPE_ANCHORS = 13;//主播圈
    public static final int VIEWTYPE_COMME_ENTRY = 14;//通用入口
    public static final int VIEWTYPE_PROMOTE = 2;//推荐新闻



    private LittleLifeManager littleLifeManager;
    private OnItemInternalClick bannerActiveClick;
    private BaseActivity activity;
    private BaseFragment fragment;
    private HomeLiveHolder.OnLivePlayingListener playingListener;
    private ArrayList<Pair<String, String>> pairs;
    private HomeSpecialHolder.OnSpecialSelectMoreListener specialListener;
    private HomeEntryHolder.OnEntryClickListener entryClickListener;
    private HomeAnchorHolder.OnAnchorSelectMoreListener anchorListener;
    private HomeAnchorEntranceHolder.OnEntranceClickListener entranceClickListener;
    private HomeAnchorHolder.OnAnchorSelectItemListener itemListener;
    private HomeHotVideoHolder.OnHotVideoSelectListener hotVideoListener;

    public void setActivity(BaseActivity activity) {
        this.activity = activity;
    }

    public void setPlayingListener(HomeLiveHolder.OnLivePlayingListener playingListener) {
        this.playingListener = playingListener;
    }

    public void setFragment(BaseFragment fragment) {
        this.fragment = fragment;
    }

    public void setPairs(ArrayList<Pair<String, String>> pairs) {
        this.pairs = pairs;
    }

    public GreatestAdapter(Context context, List list, BaseFragment fragment, HomeLiveHolder.OnLivePlayingListener listener) {
        super(context, list);
        this.fragment = fragment;
        playingListener = listener;
        littleLifeManager = new LittleLifeManager();
    }


    @Override
    public BaseAbsHolder<IListShowData> onCreateViewHolder(ViewGroup parent, int viewType) {
        BaseAbsHolder bah = null;
        View v = null;
        switch (viewType) {
            case VIEWTYPE_BANNER://轮播图
                v = LayoutInflater.from(context).inflate(R.layout.item_home_banner, parent, false);
//                JLog.e("aasa  v="+v);
                bah = new HomeBannerHolder(context, v);
                break;
            case VIEWTYPE_EXPRESS://快讯
                v = LayoutInflater.from(context).inflate(R.layout.item_home_active, parent, false);
                bah = new HomeActiveHolder(context, v);
                bah.setInternalClick(bannerActiveClick);
                break;
            case VIEWTYPE_LIVE://直播
                v = LayoutInflater.from(context).inflate(R.layout.item_home_live, parent, false);
                bah = new HomeLiveHolder(context, v, fragment, playingListener);
                break;
            case VIEWTYPE_POPULAR://热门视频
                v = LayoutInflater.from(context).inflate(R.layout.item_home_hot_video, parent, false);
                bah = new HomeHotVideoHolder(context, v, hotVideoListener);
                bah.setInternalClick(this);
                break;
            case VIEWTYPE_TITLE_24://24小时的组标题,第一个的
                v = LayoutInflater.from(context).inflate(R.layout.item_title_24, parent, false);
                bah = new Hour24TitleHolder(context, v);
                break;
            case VIEWTYPE_TITLE_24_CONTINUE://24小时的组标题,中间的
                v = LayoutInflater.from(context).inflate(R.layout.item_title_24_c, parent, false);
                bah = new Hour24TitleHolder(context, v);
                break;
            case VIEWTYPE_LUZHOU_NEWS://泸州市新闻
                v = LayoutInflater.from(context).inflate(R.layout.item_news_24_cl, parent, false);
                bah = new News24Holder(context, v);
                break;
            case VIEWTYPE_TOP1_NEWS://头顶新闻
                v = LayoutInflater.from(context).inflate(R.layout.top_news_list, parent, false);
                bah = new HomeTopNewsListHolder(context, v, 1);
                break;
            case VIEWTYPE_TOP2_NEWS://
                v = LayoutInflater.from(context).inflate(R.layout.top_news_list, parent, false);
                bah = new HomeTopNewsListHolder(context, v, 2);
                break;
            case VIEWTYPE_TOP3_NEWS://
                v = LayoutInflater.from(context).inflate(R.layout.top_news_list, parent, false);
                bah = new HomeTopNewsListHolder(context, v, 3);
                break;
            case VIEWTYPE_SPECIAL://精选-专题
                v = LayoutInflater.from(context).inflate(R.layout.item_home_hot_video, parent, false);
                bah = new HomeSpecialHolder(context, v, specialListener);
                bah.setInternalClick(this);
                break;
            case VIEWTYPE_COMME_ENTRY://精选-通用入口
                v = LayoutInflater.from(context).inflate(R.layout.item_home_entry, parent, false);
                bah = new HomeEntryHolder(context, v, entryClickListener);
                bah.setInternalClick(this);
                break;

            case VIEWTYPE_ANCHORS://精选-主播、广播、买相因入口
//                v = LayoutInflater.from(context).inflate(R.layout.item_home_anchors, parent, false);
//                bah = new HomeAnchorHolder(context, v,anchorListener,itemListener);
//                bah.setInternalClick(this);

                v = LayoutInflater.from(context).inflate(R.layout.item_home_anchor_radio_goods, parent, false);
                bah = new HomeAnchorEntranceHolder(context, v, entranceClickListener);
                break;
            case VIEWTYPE_PROMOTE://推荐头条
                v = LayoutInflater.from(context).inflate(R.layout.item_news_promote, parent, false);
                bah = new NewsPromoteHolder(context, v);
                break;

            case VIEWTYPE_NEWS_24://24小时新闻
            default:
                v = LayoutInflater.from(context).inflate(R.layout.item_news_24_cl, parent, false);
                bah = new News24Holder(context, v);
                break;
        }
        bah.setActivity(activity);
        bah.setFragment(fragment);
        bah.setPairs(pairs);
        return bah;
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getDataViewType();
    }

    @Override
    public void onBindViewHolder(BaseAbsHolder<IListShowData> holder, int position) {
        super.onBindViewHolder(holder, position);
        holder.setItemMode(position == getItemCount() - 1 ? BaseAbsHolder.ITEMMODE_LAST : BaseAbsHolder.ITEMMODE_NORMAL);
    }

    @Override
    public void onResume() {
        littleLifeManager.onResume();
    }

    @Override
    public void onPause() {
        littleLifeManager.onPause();
    }

    @Override
    public void onDestory() {
        littleLifeManager.onDestory();
    }

    protected void addLife(IAndroidLittleLife ia) {
        littleLifeManager.addLifeInf(ia);
    }

    public GreatestAdapter setActiveClick(OnItemInternalClick bannerActiveClick) {
        this.bannerActiveClick = bannerActiveClick;
        return this;
    }

    public OnItemInternalClick getActiveClick() {
        return bannerActiveClick;
    }

    @Override
    public GreatestAdapter setItemInternalClick(OnItemInternalClick itemInternalClick) {
        super.setItemInternalClick(itemInternalClick);
        return this;
    }

    @Override
    public void addDatas(List<IListShowData> list) {
        super.addDatas(list);
    }

    public void resetDatasFroms(int pos, List<? extends IListShowData> datas, boolean isForceAdd) {
        if (datas == null || datas.size() == 0) {
            return;
        }
        if (pos < 0) {
            if (isForceAdd) {
                addDatass(datas);
            }
        } else {
            int count = getItemCount();
            if (pos < count) {
                data = data.subList(0, pos);
//                JLog.e("after sub size is " + data.size());
                data.addAll(datas);
                notifyItemRangeChanged(pos, Math.max(count - pos, datas.size()));
            } else {
                addDatass(datas);
            }
        }
    }

    public void addDatass(List<? extends IListShowData> list) {
        if (data == null) {
            data = new ArrayList<>();
            data.addAll(list);
            if (data.size() > 0) {
                notifyDataSetChanged();
            }
        } else if (list != null && list.size() > 0) {
            int fromPos = getItemCount();
            if (fromPos < 0) {
                fromPos = 0;
            }
            data.addAll(list);
            notifyItemRangeChanged(fromPos, list.size());
        }
    }

    public void setDatass(List<? extends IListShowData> list) {
        data.clear();
        data.addAll(list);
        notifyDataSetChanged();
    }

    public void setSpecialListener(HomeSpecialHolder.OnSpecialSelectMoreListener specialListener) {
        this.specialListener = specialListener;
    }

    public void setAnchorListener(HomeAnchorHolder.OnAnchorSelectMoreListener anchorListener) {
        this.anchorListener = anchorListener;
    }

    public void setAnchorItemListener(HomeAnchorHolder.OnAnchorSelectItemListener itemListener) {
        this.itemListener = itemListener;
    }

    public void setEntranceClickListener(HomeAnchorEntranceHolder.OnEntranceClickListener entranceClickListener) {
        this.entranceClickListener = entranceClickListener;
    }

    public void setEntryClickListener(HomeEntryHolder.OnEntryClickListener entryClickListener) {
        this.entryClickListener = entryClickListener;
    }

    public void setHotVideoListener(HomeHotVideoHolder.OnHotVideoSelectListener hotVideoListener) {
        this.hotVideoListener = hotVideoListener;
    }
}
